import pay from './payRequset'
import request from './request'
import { Toast } from 'antd-mobile'

// 台行支付 *********************************

export const payRet = (config) => {
  // 台行支付创建订单
  return pay({
    params: {
      action: 'createOrder',
    },
    data: config
  })
}

export const payTheBalance = (config) => {
  return pay({
    params: {
      action: 'createOrder'
    },
    data: config
  })
}

export const _payTheBalance = (config) => {


  localStorage.removeItem('orderNoList')
  localStorage.removeItem('prepayOrder')

  let hash = window.location.hash
  let url = window.location.href.replace(hash, `#/handelpay/${config.orderno}/${config.tuan_id}`)
  config.url = url

  payTheBalance(config).then(res => {

    if (res.status === 200) {
      let orderNoList = res?.data?.body?.orderFlowNo
      let baseUrl = `https://pay.tzbank.com:11112/api/wc/cashier/wakeupCashier?businessCstNo=${config.uniacid}&platMerCstNo=8238509791249432688&orderNoList=${orderNoList}`;


      localStorage.setItem('orderNoList', JSON.stringify([orderNoList]))
      localStorage.setItem('prepayOrder', JSON.stringify([]))

      window.location.href = baseUrl
    } else {
      Toast.info('非法参数', 2)
    }

  })



}



//  实际支付
export const tbaBankPay = async (config, uniacid) => {
  // let uniacid = JSON.parse(localStorage.getItem('localUniacid'))

  console.log(config, uniacid)

  const { deposit_orderno, sfje, tuan_id, needprepayratio, needprepayprice } = config
  let hash = window.location.hash
  let url = window.location.href.replace(hash, `#/handelpay/${config.orderno}/${tuan_id}`)


  let prepayOrder = [] // 子订单数组，支付成功后要将该数组传给 支付回调接口
  let subOrder1 = deposit_orderno + 'y1' // 子订单 1 
  let subOrder2 = deposit_orderno + 'y2' // 子订单 2
  let order1Price = parseFloat(needprepayprice)
  let order2Price = (parseFloat(sfje) - order1Price)

  let params = {
    uniacid,
    orderno: subOrder1,
    price: sfje,
    url
  }

  if (needprepayratio !== 0) {
    // 需要预付款
    //  产生两个子订单  分两次请求
    params.price = order1Price
    params.orderno = subOrder1

    payRet(params).then(res => {

      if (res.status === 200) {
        let orderNoList1 = res.data.body.orderFlowNo
        params.orderno = subOrder2
        params.price = order2Price

        payRet(params).then(res => {
          if (res.status === 200) {
            let orderNoList2 = res.data.body.orderFlowNo
            let baseUrl = `https://pay.tzbank.com:11112/api/wc/cashier/wakeupCashier?businessCstNo=${uniacid}&platMerCstNo=8238509791249432688&orderNoList=${orderNoList1},${orderNoList2}`;

            console.log(baseUrl)

            prepayOrder.push({ [subOrder1]: order1Price })
            prepayOrder.push({ [subOrder2]: order2Price })
            localStorage.setItem('orderNoList', JSON.stringify([orderNoList1, orderNoList2]))
            localStorage.setItem('prepayOrder', JSON.stringify(prepayOrder))

            window.location.href = baseUrl
          } else {
            Toast.fail('非法参数', 1)
          }
        })
      }
    })
  } else {
    //  不需要预付款
    let res = await payRet(params)

    console.log(res)

    if (res.status === 200) {
      let orderNoList = res?.data?.body?.orderFlowNo
      let baseUrl = `https://pay.tzbank.com:11112/api/wc/cashier/wakeupCashier?businessCstNo=${uniacid}&platMerCstNo=8238509791249432688&orderNoList=${orderNoList}`;


      prepayOrder.push({ [subOrder1]: sfje })
      localStorage.setItem('orderNoList', JSON.stringify([orderNoList]))
      localStorage.setItem('prepayOrder', JSON.stringify(prepayOrder))
      window.location.href = baseUrl

    } else {
      Toast.fail('非法参数', 1)
    }

  }


}


//  火蝶这边的 ******************************
export const payReturn = (config) => {
  // 保证金支付毁掉
  return request({
    params: {
      action: 'pay_return',
    },
    data: config
  })
}

export const getGroupFirstAddress = (config) => (
  request({
    params: {
      action: 'group_first_address',
    },
    data: config
  })
)

export const detactGroupIsFull = config => (
  request({
    params: {
      action: 'findTuan'
    },
    data: config
  })
)

